package com.example.gui.javafxdemo;

import javafx.application.Platform;
import javafx.scene.web.WebEngine;
import netscape.javascript.JSObject;

public class JsAdapter {
    private final WebEngine engine;

    public JsAdapter(WebEngine engine) {
        this.engine = engine;
    }

    // JS 点击图表时调用
    public void onChartClick(int index) {
        Platform.runLater(() -> {
            javafx.scene.control.Alert alert =
                new javafx.scene.control.Alert(javafx.scene.control.Alert.AlertType.INFORMATION,
                    "你点击了第 " + index + " 个点！");
            alert.setHeaderText(null);
            alert.show();
        });
    }

    /* Java 主动推数据给 JS */
    public void pushData(int value) {
        Platform.runLater(() ->
            engine.executeScript("updateData(" + value + ");")
        );
    }
}